package dao;

import hbt.HibernateUtil;

import java.util.List;





import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Dominio.Cliente;
import Dominio.Proveedor;


public class ClienteDAO {

	protected static ClienteDAO instancia = null;
	protected static SessionFactory sf = null;
	protected static Session session = null;

	public static ClienteDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new ClienteDAO();
		} 
		return instancia;
	}
	
	private Session getSession(){
		if(session == null)
			session = sf.openSession();
		return session;
	}
	
	
	public void closeSession()
	{
		if (session.isOpen()) 
		{
			session.close();
		}
	}

	/////DESDE ACA VAN LOS METODOS
	public void agregarCliente(Cliente cli)
	{
		try
		{
			Session s=getSession();
			s.beginTransaction();
			s.persist(cli);
			s.flush();
			s.getTransaction().commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public String getUltimoId (){
		Session s=getSession();
		String o=(String) s.createQuery("select max(cli.id) from Cliente cli").uniqueResult().toString();
		return o;	
	}
	
	public void eliminarCliente (Cliente cli){
			Session s=getSession();
			s.beginTransaction();
			s.delete(cli);
			s.flush();
			s.getTransaction().commit();
	}
	
	public Cliente loadCliente(int id){
		Cliente cli = new Cliente();
		Session s=this.getSession();

		s.load(cli, id);
		return cli;	
	}
	
	@SuppressWarnings("unchecked")
	public List<Cliente> listarClientes()
	{
		Session s = this.getSession();
		List<Cliente> lista= s.createQuery("from Cliente order by id").list();
		return lista;
	}
	
	public void modificarCliente (Cliente cliente){
		Session s= getSession();
		s.beginTransaction();
		s.update(cliente);  
		s.flush();
		s.getTransaction().commit();
	}
	
	@SuppressWarnings("unchecked")
	public List<String> listaId(){
		Session s=getSession();
		List<String> lista=s.createQuery("select cli.id from Cliente cli").list();
		s.clear();
		return lista;
	}
	
	public Cliente buscarCliente(int id){
		Session s= getSession();
		Cliente cli=(Cliente) s.getNamedQuery("imprimir cliente").setInteger("id", id).uniqueResult();
		s.clear();
		return cli;
	}
	
	public String imprimirCliente(int id){
		Session s= getSession();
		
		Cliente cli= (Cliente) s.getNamedQuery("imprimir cliente").setInteger("id", id).uniqueResult();
		
		String str;
		if(cli!=null){
		
		 str= "numero de cliente: "+cli.getIdCliente()+"\nrazon social: "+cli.getRazonSocial()+"\ncalle: "+
						cli.getDomicilio().getCalle()+"\nnumero: "+cli.getDomicilio().getNumero()+"\npiso: "+
						cli.getDomicilio().getPiso()+"\ndepartamento: "+cli.getDomicilio().getDepartamento()+"\nlocalidad: "+
						cli.getDomicilio().getLocalidad()+"telefono: "+cli.getTelefonos().get(0).getArea() + "-" + cli.getTelefonos().get(0).getNumero();
		}
		else
			str=null;
			
		s.clear();
		return str;
	}
	
	  /******************************requerimiento numero 1 ***************************************************/
/////VER SI SIRVEN	
	public boolean esCliente(int id){
		Session s= getSession();
		Cliente cli=(Cliente) s.getNamedQuery("imprimir cliente").setInteger("id", id).uniqueResult();
		if(cli!=null)
		return true;
		else
			return false;
	}
	
	public boolean coincideIdRz(int id ,String RazonSocial){
		Session s = getSession();
		Object o = s.getNamedQuery("coincideIdRz").setInteger("id", id).setString("rz", RazonSocial).uniqueResult();
		if(o==null)
			return false;
		else
			return true;
	}
	
	public boolean existeNs(String ns){
		Session s= sf.openSession();
		Object o= s.getNamedQuery("existeNs").setString("ns", ns).uniqueResult();
		if(o==null)
			return false;
		else
			return true;
		
	}
	
	public Cliente ClienteConMasFacturas() {
		Session s= sf.openSession();
		
		//String query = "SELECT fac.cliente, COUNT(fac.id) AS tot FROM Factura fac GROUP BY fac.cliente.id order by tot desc";
		//String query = "SELECT TOP 1 cliente.id,COUNT(*) TOT FROM Factura fac join fac.cliente cliente group by cliente.id order by TOT DESC";// fac join fac.cliente";
		String query = "from Factura";
		
		//traigo al cliente entero
		long a = (Long) s.createQuery(query).uniqueResult();
		//List<String> ser = (List<String>)s.createQuery(query).uniqueResult();
		System.out.println(a);
		
		return null;
	}

	public Cliente buscarClientePorCUIL(String cuil) {
		try
		{
			Session s = getSession();
			String query = "select cli from Cliente cli where cli.cuil = :cuil";
			Cliente cliente = (Cliente) s.createQuery(query).setParameter("cuil", cuil).uniqueResult();
			s.clear();
			//System.out.println(cliente.getCuil() + cliente.getRazonSocial());
			return cliente;
		}
		catch(Exception e)
		{
			return null;
		}
	}

}